package aes.gui;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import aes.general.GuiControlBeta;
import aes.general.Msg;
import aes.general.StudentControl;
import aes.general.TeacherControl;

import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;

public class StudentGradesPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JLabel l1;
	private JButton b1;
	private TableModel data;
	private JPanel TablePanel;
	
	private Vector<Vector<String>> grades;
	private ArrayList<String> exameInfo = new ArrayList<String>();
	private JTable table;
	private ListSelectionModel lsm;
	private JButton btndonwload;
	
	/**
	 * Create the panel.
	 * @param grades2 
	 */
	public StudentGradesPanel(JPanel contentPane, Vector<Vector<String>> grades2) {
		this.jp = contentPane;
		grades = grades2;
		setLayout(null);
		initialize();
	}

	public void initialize(){
		l1 = new JLabel("Your Grades and Exams");
		l1.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		l1.setBounds(197, 11, 245, 29);
		add(l1);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 38, 620, 2);
		add(separator);
		
		
		b1 = new JButton("Back");
		b1.setBounds(169, 470, 132, 23);
		add(b1);
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StudentControl.setGradesvector(null);
				CardLayout cl = (CardLayout)(jp.getLayout());
				 cl.show(jp,"MainPanel");	
			}
		});
		
		TablePanel = new JPanel();
		TablePanel.setBounds(46, 71, 517, 351);
		add(TablePanel);
		
		
		
			data = new AbstractTableModel() {
			@Override
			public Object getValueAt(int rowIndex, int columnIndex) {
				
				return grades.get(rowIndex).get(columnIndex);
			}
			
			@Override
			public int getRowCount() {
				
				return grades.size();
			}
			
			@Override
			public int getColumnCount() {
				
				return grades.get(0).size();
			}
			public String getColumnName(int column){
				String[] title = {"Exame ID","Date","grade","Course"};
				return title[column];
			}
		};
		TablePanel.setLayout(null);
		table = new JTable(data);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		JScrollPane grdscroll = new JScrollPane(table);
		grdscroll.setBounds(32, 5, 452, 323);
		grdscroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		TablePanel.add(grdscroll);
		
		btndonwload = new JButton("View Exam");
		btndonwload.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lsm  = table.getSelectionModel();
				if(!lsm.isSelectionEmpty()){
				int rowSelected = table.getSelectedRow();
				
				exameInfo.add(GuiControlBeta.userid);
				exameInfo.add((String) table.getModel().getValueAt(rowSelected, 0));
				StudentControl.RecieveFinishedExamInfo(exameInfo);
				StudentViewExamPanel avep = new StudentViewExamPanel(jp);
				jp.add(avep,"VeiwExam");
				CardLayout cl = (CardLayout)(jp.getLayout());
				cl.show(jp,"VeiwExam");
				}
				else
				{
					JOptionPane.showMessageDialog(null, "Error,No Selection", "Error",
	    					JOptionPane.ERROR_MESSAGE);	
				}
			}
		});
		btndonwload.setBounds(332, 470, 132, 23);
		add(btndonwload);
	}
	
}
